// import { effects } from 'dva-core/saga';
import { history } from 'umi';
import menu from '@/config/menu.json';
import { delay } from '@/utils/common';
export default {
  namespace: '$router',
  state: {
    active: '',
    list: menu.list,
  },
  reducers: {
    save(state, { payload }) {
      return Object.assign({}, state, payload);
    },
  },
  effects: {
    *forward({ payload }, { put, call, select }) {
      const { url } = payload;
      yield put({
        type: 'save',
        payload: {
          active: url,
        },
      });
      yield history.push('/' + url);
    },
    *redirect({ payload }, { put, call, select }) {
      yield history.replace(payload.url);
      yield put({
        type: 'save',
        payload: {
          active: url,
        },
      });
    },
    *back({ payload }, { put, call, select }) {
      yield history.goBack();
      yield call(delay, 100);
      const url = location.hash.slice(2);
      yield put({
        type: 'save',
        payload: {
          active: url,
        },
      });
    },
  },
};
